package com.facebook.imageformat;

import com.facebook.common.internal.ByteStreams;
import com.facebook.common.internal.Ints;
import com.facebook.common.internal.Preconditions;
import com.facebook.common.internal.Throwables;
import com.facebook.common.webp.WebpSupportStatus;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

public class ImageFormatChecker
{
  private static final byte[] BMP_HEADER;
  private static final int EXTENDED_WEBP_HEADER_LENGTH = 21;
  private static final byte[] GIF_HEADER_87A;
  private static final byte[] GIF_HEADER_89A;
  private static final int GIF_HEADER_LENGTH = 6;
  private static final byte[] JPEG_HEADER = { -1, -40, -1 };
  private static final int MAX_HEADER_LENGTH = 0;
  private static final byte[] PNG_HEADER = { -119, 80, 78, 71, 13, 10, 26, 10 };
  private static final int SIMPLE_WEBP_HEADER_LENGTH = 20;

  static
  {
    GIF_HEADER_87A = asciiBytes("GIF87a");
    GIF_HEADER_89A = asciiBytes("GIF89a");
    BMP_HEADER = asciiBytes("BM");
    int[] arrayOfInt = new int[6];
    arrayOfInt[0] = 21;
    arrayOfInt[1] = 20;
    arrayOfInt[2] = JPEG_HEADER.length;
    arrayOfInt[3] = PNG_HEADER.length;
    arrayOfInt[4] = 6;
    arrayOfInt[5] = BMP_HEADER.length;
  }

  private static byte[] asciiBytes(String paramString)
  {
    Preconditions.checkNotNull(paramString);
    try
    {
      byte[] arrayOfByte = paramString.getBytes("ASCII");
      return arrayOfByte;
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      throw new RuntimeException("ASCII not found!", localUnsupportedEncodingException);
    }
  }

  private static ImageFormat doGetImageFormat(byte[] paramArrayOfByte, int paramInt)
  {
    Preconditions.checkNotNull(paramArrayOfByte);
    if (WebpSupportStatus.isWebpHeader(paramArrayOfByte, 0, paramInt))
      return getWebpFormat(paramArrayOfByte, paramInt);
    if (isJpegHeader(paramArrayOfByte, paramInt))
      return ImageFormat.JPEG;
    if (isPngHeader(paramArrayOfByte, paramInt))
      return ImageFormat.PNG;
    if (isGifHeader(paramArrayOfByte, paramInt))
      return ImageFormat.GIF;
    if (isBmpHeader(paramArrayOfByte, paramInt))
      return ImageFormat.BMP;
    return ImageFormat.UNKNOWN;
  }

  public static ImageFormat getImageFormat(InputStream paramInputStream)
    throws IOException
  {
    Preconditions.checkNotNull(paramInputStream);
    byte[] arrayOfByte = new byte[MAX_HEADER_LENGTH];
    return doGetImageFormat(arrayOfByte, readHeaderFromStream(paramInputStream, arrayOfByte));
  }

  // ERROR //
  public static ImageFormat getImageFormat(String paramString)
  {
    // Byte code:
    //   0: aconst_null
    //   1: astore_1
    //   2: new 138	java/io/FileInputStream
    //   5: dup
    //   6: aload_0
    //   7: invokespecial 141	java/io/FileInputStream:<init>	(Ljava/lang/String;)V
    //   10: astore_2
    //   11: aload_2
    //   12: invokestatic 143	com/facebook/imageformat/ImageFormatChecker:getImageFormat	(Ljava/io/InputStream;)Lcom/facebook/imageformat/ImageFormat;
    //   15: astore 6
    //   17: aload_2
    //   18: invokestatic 149	com/facebook/common/internal/Closeables:closeQuietly	(Ljava/io/InputStream;)V
    //   21: aload 6
    //   23: areturn
    //   24: astore 7
    //   26: getstatic 125	com/facebook/imageformat/ImageFormat:UNKNOWN	Lcom/facebook/imageformat/ImageFormat;
    //   29: astore 5
    //   31: aload_1
    //   32: invokestatic 149	com/facebook/common/internal/Closeables:closeQuietly	(Ljava/io/InputStream;)V
    //   35: aload 5
    //   37: areturn
    //   38: astore 4
    //   40: aload_1
    //   41: invokestatic 149	com/facebook/common/internal/Closeables:closeQuietly	(Ljava/io/InputStream;)V
    //   44: aload 4
    //   46: athrow
    //   47: astore 4
    //   49: aload_2
    //   50: astore_1
    //   51: goto -11 -> 40
    //   54: astore_3
    //   55: aload_2
    //   56: astore_1
    //   57: goto -31 -> 26
    //
    // Exception table:
    //   from	to	target	type
    //   2	11	24	java/io/IOException
    //   2	11	38	finally
    //   26	31	38	finally
    //   11	17	47	finally
    //   11	17	54	java/io/IOException
  }

  public static ImageFormat getImageFormat_WrapIOException(InputStream paramInputStream)
  {
    try
    {
      ImageFormat localImageFormat = getImageFormat(paramInputStream);
      return localImageFormat;
    }
    catch (IOException localIOException)
    {
      throw Throwables.propagate(localIOException);
    }
  }

  private static ImageFormat getWebpFormat(byte[] paramArrayOfByte, int paramInt)
  {
    Preconditions.checkArgument(WebpSupportStatus.isWebpHeader(paramArrayOfByte, 0, paramInt));
    if (WebpSupportStatus.isSimpleWebpHeader(paramArrayOfByte, 0))
      return ImageFormat.WEBP_SIMPLE;
    if (WebpSupportStatus.isLosslessWebpHeader(paramArrayOfByte, 0))
      return ImageFormat.WEBP_LOSSLESS;
    if (WebpSupportStatus.isExtendedWebpHeader(paramArrayOfByte, 0, paramInt))
    {
      if (WebpSupportStatus.isAnimatedWebpHeader(paramArrayOfByte, 0))
        return ImageFormat.WEBP_ANIMATED;
      if (WebpSupportStatus.isExtendedWebpHeaderWithAlpha(paramArrayOfByte, 0))
        return ImageFormat.WEBP_EXTENDED_WITH_ALPHA;
      return ImageFormat.WEBP_EXTENDED;
    }
    return ImageFormat.UNKNOWN;
  }

  private static boolean isBmpHeader(byte[] paramArrayOfByte, int paramInt)
  {
    if (paramInt < BMP_HEADER.length)
      return false;
    return matchBytePattern(paramArrayOfByte, 0, BMP_HEADER);
  }

  private static boolean isGifHeader(byte[] paramArrayOfByte, int paramInt)
  {
    if (paramInt < 6);
    while ((!matchBytePattern(paramArrayOfByte, 0, GIF_HEADER_87A)) && (!matchBytePattern(paramArrayOfByte, 0, GIF_HEADER_89A)))
      return false;
    return true;
  }

  private static boolean isJpegHeader(byte[] paramArrayOfByte, int paramInt)
  {
    int i = JPEG_HEADER.length;
    boolean bool1 = false;
    if (paramInt >= i)
    {
      boolean bool2 = matchBytePattern(paramArrayOfByte, 0, JPEG_HEADER);
      bool1 = false;
      if (bool2)
        bool1 = true;
    }
    return bool1;
  }

  private static boolean isPngHeader(byte[] paramArrayOfByte, int paramInt)
  {
    int i = PNG_HEADER.length;
    boolean bool1 = false;
    if (paramInt >= i)
    {
      boolean bool2 = matchBytePattern(paramArrayOfByte, 0, PNG_HEADER);
      bool1 = false;
      if (bool2)
        bool1 = true;
    }
    return bool1;
  }

  private static boolean matchBytePattern(byte[] paramArrayOfByte1, int paramInt, byte[] paramArrayOfByte2)
  {
    Preconditions.checkNotNull(paramArrayOfByte1);
    Preconditions.checkNotNull(paramArrayOfByte2);
    if (paramInt >= 0);
    for (boolean bool = true; ; bool = false)
    {
      Preconditions.checkArgument(bool);
      if (paramInt + paramArrayOfByte2.length <= paramArrayOfByte1.length)
        break;
      return false;
    }
    for (int i = 0; ; i++)
    {
      if (i >= paramArrayOfByte2.length)
        break label68;
      if (paramArrayOfByte1[(i + paramInt)] != paramArrayOfByte2[i])
        break;
    }
    label68: return true;
  }

  private static int readHeaderFromStream(InputStream paramInputStream, byte[] paramArrayOfByte)
    throws IOException
  {
    Preconditions.checkNotNull(paramInputStream);
    Preconditions.checkNotNull(paramArrayOfByte);
    boolean bool;
    if (paramArrayOfByte.length >= MAX_HEADER_LENGTH)
      bool = true;
    while (true)
    {
      Preconditions.checkArgument(bool);
      if (paramInputStream.markSupported())
        try
        {
          paramInputStream.mark(MAX_HEADER_LENGTH);
          int i = ByteStreams.read(paramInputStream, paramArrayOfByte, 0, MAX_HEADER_LENGTH);
          return i;
          bool = false;
        }
        finally
        {
          paramInputStream.reset();
        }
    }
    return ByteStreams.read(paramInputStream, paramArrayOfByte, 0, MAX_HEADER_LENGTH);
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.facebook.imageformat.ImageFormatChecker
 * JD-Core Version:    0.6.2
 */